<template>
  <div class="hotel-list">
    <div class="hotel-list-header">
      <h2>酒店预订</h2>
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="搜索酒店名称"
          prefix-icon="el-icon-search"
          clearable
          @clear="clearSearch"
          @keyup.enter.native="handleSearch"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 搜索结果提示 -->
    <div class="search-result" v-if="hasSearched && searchQuery">
      <span>搜索结果：共找到 {{ hotels.length }} 家酒店</span>
      <el-button type="text" @click="clearSearch">清除搜索</el-button>
    </div>

    <!-- 酒店列表 -->
    <div class="hotel-grid">
      <el-row :gutter="20" type="flex" justify="start">
        <el-col 
          :xs="24" 
          :sm="12" 
          :md="8" 
          :lg="8" 
          v-for="(hotel, index) in paginatedHotels" 
          :key="hotel.id"
        >
          <el-card class="hotel-card" shadow="hover">
            <div class="hotel-image">
              <img :src="getHotelImage(index)" :alt="hotel.name">
            </div>
            <div class="hotel-content">
              <h3 class="hotel-name">{{ hotel.name }}</h3>
              <div class="hotel-rating">
                <el-rate
                  v-model="hotel.rating"
                  disabled
                  show-score
                  text-color="#ff9900"
                />
              </div>
              <div class="hotel-info">
                <p class="hotel-address">
                  <i class="el-icon-location"></i>
                  {{ hotel.address }}
                </p>
                <p class="hotel-phone">
                  <i class="el-icon-phone"></i>
                  {{ hotel.phone }}
                </p>
                <p class="hotel-desc">{{ truncateDescription(hotel.description) }}</p>
              </div>
              <div class="hotel-actions">
                <el-button type="primary" size="small" @click="handleBook(hotel)">立即预订</el-button>
                <el-button size="small" @click="showDetails(hotel)">查看详情</el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 无数据提示 -->
    <el-empty 
      v-if="hotels.length === 0" 
      description="暂无酒店信息"
    ></el-empty>

    <!-- 分页器 -->
    <div class="pagination-container" v-if="hotels.length > 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="hotels.length"
      >
      </el-pagination>
    </div>

    <!-- 酒店详情对话框 -->
    <el-dialog
      :title="selectedHotel ? selectedHotel.name : '酒店详情'"
      :visible.sync="dialogVisible"
      width="60%"
      custom-class="hotel-detail-dialog"
    >
      <div v-if="selectedHotel" class="hotel-detail">
        <div class="detail-image">
          <img :src="getHotelImage(selectedHotelIndex)" :alt="selectedHotel.name">
        </div>
        <div class="detail-content">
          <div class="detail-rating">
            <el-rate
              v-model="selectedHotel.rating"
              disabled
              show-score
              text-color="#ff9900"
            />
          </div>
          <div class="detail-info">
            <p class="detail-address">
              <i class="el-icon-location"></i>
              {{ selectedHotel.address }}
            </p>
            <p class="detail-phone">
              <i class="el-icon-phone"></i>
              {{ selectedHotel.phone }}
            </p>
            <div class="detail-desc">
              {{ selectedHotel.description }}
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleBook(selectedHotel)">立即预订</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'HotelList',
  data() {
    return {
      hotels: [],
      currentPage: 1,
      pageSize: 10,
      searchQuery: '',
      selectedHotel: null,
      dialogVisible: false,
      loading: false,
      hasSearched: false,
      hotelImages: [
        'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=800&h=500', // 豪华酒店大堂
        'https://images.unsplash.com/photo-1582719508461-905c673771fd?w=800&h=500', // 现代酒店外观
        'https://images.unsplash.com/photo-1611892440504-42a792e24d32?w=800&h=500', // 豪华套房
        'https://images.unsplash.com/photo-1618773928121-c32242e63f39?w=800&h=500', // 酒店房间
        'https://images.unsplash.com/photo-1571896349842-33c89424de2d?w=800&h=500', // 酒店泳池
        'https://images.unsplash.com/photo-1520250497591-112f2f40a3f4?w=800&h=500', // 酒店餐厅
        'https://images.unsplash.com/photo-1584132967334-10e028bd69f7?w=800&h=500', // 酒店大床房
        'https://images.unsplash.com/photo-1578683010236-d716f9a3f461?w=800&h=500', // 酒店浴室
        'https://images.unsplash.com/photo-1551882547-ff40c63fe5fa?w=800&h=500',    // 酒店双床房
        'https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?w=800&h=500'  // 酒店休息区
      ]
    }
  },
  computed: {
    paginatedHotels() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.hotels.slice(start, end);
    },
    selectedHotelIndex() {
      return this.hotels.findIndex(h => h.id === this.selectedHotel?.id)
    }
  },
  created() {
    this.fetchHotels();
  },
  methods: {
    async fetchHotels() {
      try {
        this.loading = true;
        const response = await request.get('/api/hotels');
        this.hotels = response || [];
        this.hasSearched = false;
      } catch (error) {
        console.error('获取酒店列表失败:', error);
        this.$message.error('获取酒店列表失败');
        this.hotels = [];
      } finally {
        this.loading = false;
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      const maxPage = Math.ceil(this.hotels.length / this.pageSize);
      if (this.currentPage > maxPage) {
        this.currentPage = 1;
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    async handleSearch() {
      if (!this.searchQuery.trim()) {
        await this.fetchHotels();
        return;
      }
      
      try {
        this.loading = true;
        const response = await request.get('/api/hotels', {
          params: { keyword: this.searchQuery.trim() }
        });
        this.hotels = response || [];
        this.currentPage = 1;
        this.hasSearched = true;
      } catch (error) {
        console.error('搜索失败:', error);
        this.$message.error('搜索失败');
        this.hotels = [];
      } finally {
        this.loading = false;
      }
    },
    async clearSearch() {
      this.searchQuery = '';
      this.hasSearched = false;
      await this.fetchHotels();
      this.currentPage = 1;
    },
    showDetails(hotel) {
      this.selectedHotel = hotel;
      this.dialogVisible = true;
    },
    handleBook(hotel) {
      // TODO: 实现预订功能
      this.$message.info('预订功能开发中...');
    },
    truncateDescription(description) {
      if (!description) return '';
      return description.length > 50 ? description.substring(0, 50) + '...' : description;
    },
    getHotelImage(index) {
      return this.hotelImages[index % this.hotelImages.length]
    }
  }
}
</script>

<style scoped>
.hotel-list {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.hotel-list-header {
  margin-bottom: 20px;
}

.hotel-list-header h2 {
  margin: 0 0 20px;
  color: #303133;
  font-size: 24px;
}

.search-bar {
  max-width: 500px;
}

.search-result {
  background: #fff;
  padding: 12px 20px;
  border-radius: 4px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hotel-grid {
  margin: 0 -10px;
}

.el-col {
  padding: 10px;
  display: flex;
}

.hotel-card {
  width: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}

.hotel-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.hotel-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.hotel-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.hotel-name {
  margin: 0 0 10px;
  font-size: 18px;
  color: #303133;
}

.hotel-rating {
  margin-bottom: 15px;
}

.hotel-info {
  flex: 1;
}

.hotel-address, .hotel-phone {
  margin: 8px 0;
  color: #606266;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.hotel-desc {
  color: #909399;
  font-size: 14px;
  line-height: 1.4;
  margin: 10px 0;
}

.hotel-actions {
  margin-top: auto;
}

.pagination-container {
  background: white;
  padding: 15px;
  border-radius: 4px;
  display: flex;
  justify-content: center;
}

/* 详情对话框样式 */
.hotel-detail-dialog {
  border-radius: 8px;
}

.hotel-detail {
  padding: 20px;
}

.detail-image {
  width: 100%;
  height: 300px;
  overflow: hidden;
  border-radius: 4px;
  margin-bottom: 20px;
}

.detail-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.detail-content {
  padding: 0 20px;
}

.detail-rating {
  margin-bottom: 20px;
}

.detail-info {
  color: #606266;
}

.detail-desc {
  margin-top: 15px;
  line-height: 1.6;
  color: #303133;
}

/* 响应式布局调整 */
@media screen and (max-width: 768px) {
  .hotel-grid {
    margin: 0 -5px;
  }
  
  .el-col {
    padding: 5px;
  }
  
  .hotel-image {
    height: 150px;
  }
}
</style> 